<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>添加朋友 - 朋友管理系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: #f5f7fa;
            color: #333;
        }
        
        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 20px 0;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }
        
        .header-content {
            max-width: 1200px;
            margin: 0 auto;
            padding: 0 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .logo-section {
            display: flex;
            align-items: center;
            gap: 15px;
        }
        
        .logo {
            width: 40px;
            height: 40px;
            filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.1));
        }
        
        .header h1 {
            font-size: 24px;
            font-weight: 500;
        }
        
        .back-btn {
            background: rgba(255, 255, 255, 0.2);
            color: white;
            border: 1px solid rgba(255, 255, 255, 0.3);
            padding: 8px 16px;
            border-radius: 6px;
            cursor: pointer;
            text-decoration: none;
            transition: all 0.3s ease;
        }
        
        .back-btn:hover {
            background: rgba(255, 255, 255, 0.3);
        }
        
        .main-content {
            max-width: 800px;
            margin: 30px auto;
            padding: 0 20px;
        }
        
        .form-container {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
        }
        
        .form-title {
            text-align: center;
            margin-bottom: 30px;
            color: #333;
            font-size: 20px;
        }
        
        .form-row {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 20px;
            margin-bottom: 20px;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        .form-group.full-width {
            grid-column: 1 / -1;
        }
        
        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #333;
            font-weight: 500;
        }
        
        .form-group input,
        .form-group select,
        .form-group textarea {
            width: 100%;
            padding: 12px 15px;
            border: 2px solid #e1e5e9;
            border-radius: 8px;
            font-size: 14px;
            transition: border-color 0.3s ease;
            font-family: inherit;
        }
        
        .form-group input:focus,
        .form-group select:focus,
        .form-group textarea:focus {
            outline: none;
            border-color: #667eea;
        }
        
        .form-group textarea {
            resize: vertical;
            min-height: 100px;
        }
        
        .form-actions {
            display: flex;
            gap: 15px;
            justify-content: center;
            margin-top: 30px;
        }
        
        /* 头像上传样式 */
        .avatar-upload {
            display: flex;
            flex-direction: column;
            align-items: center;
            gap: 10px;
        }
        
        .avatar-preview {
            width: 80px;
            height: 80px;
            border-radius: 50%;
            overflow: hidden;
            border: 2px solid #e1e5e9;
            background: #f8f9fa;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        
        .avatar-preview img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }
        
        .avatar-input {
            display: none;
        }
        
        .avatar-upload-btn {
            background: #667eea;
            color: white;
            padding: 8px 16px;
            border-radius: 6px;
            cursor: pointer;
            font-size: 14px;
            transition: all 0.3s ease;
            border: none;
        }
        
        .avatar-upload-btn:hover {
            background: #5a6fd8;
        }
        
        .btn {
            padding: 12px 30px;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            font-size: 16px;
            font-weight: 500;
            transition: all 0.3s ease;
            text-decoration: none;
            display: inline-block;
            text-align: center;
        }
        
        .btn-primary {
            background: #667eea;
            color: white;
        }
        
        .btn-primary:hover {
            background: #5a6fd8;
            transform: translateY(-2px);
        }
        
        .btn-secondary {
            background: #95a5a6;
            color: white;
        }
        
        .btn-secondary:hover {
            background: #7f8c8d;
        }
        
        .error-message {
            color: #e74c3c;
            text-align: center;
            margin-top: 15px;
            font-size: 14px;
            display: none;
        }
        
        .success-message {
            color: #27ae60;
            text-align: center;
            margin-top: 15px;
            font-size: 14px;
            display: none;
        }
        
        @media (max-width: 768px) {
            .form-row {
                grid-template-columns: 1fr;
            }
            
            .form-actions {
                flex-direction: column;
            }
            
            .btn {
                width: 100%;
            }
        }
    </style>
</head>
<body>
    <div class="header">
        <div class="header-content">
            <div class="logo-section">
                <img src="/static/images/logo-simple.svg" alt="朋友管理系统" class="logo">
                <h1>添加朋友</h1>
            </div>
            <a href="/friend/list" class="back-btn">返回列表</a>
        </div>
    </div>
    
    <div class="main-content">
        <div class="form-container">
            <h2 class="form-title">填写朋友信息</h2>
            
            <form id="addFriendForm">
                <div class="form-row">
                    <div class="form-group">
                        <label class="form-label">姓名 *</label>
                        <input type="text" class="form-input" id="name" name="name" required>
                    </div>
                    
                    <div class="form-group">
                        <label class="form-label">头像</label>
                        <div class="avatar-upload">
                            <div class="avatar-preview" id="avatarPreview">
                                <img id="avatarImage" src="/static/images/default-avatar.svg" alt="默认头像" onerror="this.src='/static/images/default-avatar.svg'">
                            </div>
                            <input type="file" id="avatarFile" name="avatar" accept="image/*" class="avatar-input">
                            <label for="avatarFile" class="avatar-upload-btn">选择图片</label>
                        </div>
                    </div>
                </div>
                
                <div class="form-row">
                    <div class="form-group">
                        <label for="email">邮箱</label>
                        <input type="email" id="email" name="email" placeholder="请输入邮箱地址">
                    </div>
                    
                    <div class="form-group">
                        <label for="phone">电话</label>
                        <input type="tel" id="phone" name="phone" placeholder="请输入电话号码">
                    </div>
                </div>
                
                <div class="form-group full-width">
                    <label for="address">地址</label>
                    <input type="text" id="address" name="address" placeholder="请输入详细地址">
                </div>
                
                <div class="form-row">
                    <div class="form-group">
                        <label for="birthday">生日</label>
                        <input type="date" id="birthday" name="birthday">
                    </div>
                    
                    <div class="form-group">
                        <label for="school">学校</label>
                        <input type="text" id="school" name="school" placeholder="请输入学校名称">
                    </div>
                </div>
                
                <div class="form-group full-width">
                    <label for="notes">备注</label>
                    <textarea id="notes" name="notes" placeholder="请输入备注信息"></textarea>
                </div>
                
                <div class="form-actions">
                    <button type="submit" class="btn btn-primary">保存朋友</button>
                    <a href="/friend/list" class="btn btn-secondary">取消</a>
                </div>
            </form>
            
            <div class="error-message" id="errorMessage"></div>
            <div class="success-message" id="successMessage"></div>
        </div>
    </div>
    
    <script>
        document.getElementById('addFriendForm').addEventListener('submit', function(e) {
            e.preventDefault();
            
            // 获取表单数据
            const formData = {
                name: document.getElementById('name').value.trim(),
                email: document.getElementById('email').value.trim(),
                phone: document.getElementById('phone').value.trim(),
                address: document.getElementById('address').value.trim(),
                birthday: document.getElementById('birthday').value,
                notes: document.getElementById('notes').value.trim(),
                avatar: selectedAvatar || '', // 使用选中的头像或空字符串
                school: document.getElementById('school').value.trim()
            };
            
            // 验证必填字段
            if (!formData.name) {
                showMessage('请输入朋友姓名', 'error');
                return;
            }
            
            // 发送请求
            fetch('/friend/create', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify(formData)
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    showMessage(data.message, 'success');
                    setTimeout(() => {
                        window.location.href = '/friend/list';
                    }, 1500);
                } else {
                    showMessage(data.message, 'error');
                }
            })
            .catch(error => {
                console.error('Error:', error);
                showMessage('保存失败，请稍后重试', 'error');
            });
        });
        
        function showMessage(message, type) {
            const errorDiv = document.getElementById('errorMessage');
            const successDiv = document.getElementById('successMessage');
            
            if (type === 'error') {
                errorDiv.textContent = message;
                errorDiv.style.display = 'block';
                successDiv.style.display = 'none';
            } else {
                successDiv.textContent = message;
                successDiv.style.display = 'block';
                errorDiv.style.display = 'none';
            }
        }
        
        // 设置默认生日为今天
        document.getElementById('birthday').value = new Date().toISOString().split('T')[0];
        
        // 头像上传功能
        let selectedAvatar = null; // 存储选中的头像数据
        
        document.getElementById('avatarFile').addEventListener('change', function(e) {
            const file = e.target.files[0];
            if (file) {
                // 验证文件类型
                if (!file.type.startsWith('image/')) {
                    alert('请选择图片文件');
                    return;
                }
                
                // 验证文件大小（限制为2MB）
                if (file.size > 2 * 1024 * 1024) {
                    alert('图片大小不能超过2MB');
                    return;
                }
                
                // 预览图片并转换为Base64
                const reader = new FileReader();
                reader.onload = function(e) {
                    const base64Data = e.target.result;
                    document.getElementById('avatarImage').src = base64Data;
                    selectedAvatar = base64Data; // 保存Base64数据
                };
                reader.readAsDataURL(file);
            }
        });
    </script>
</body>
</html>
